// 构造+原型还是使用ES6类来写
function MyPromise(fn) {
  console.log(fn)
  // Promise只有2各种状态 一种是 pending 还是一种终止状态 resolved rejected失败
  // let promiseStatus = 'pending'
  // let RESOLVED = 'resolved'
  // let REJECTED = 'rejected'
  this.Callbacks= [] // [{resolve: function () {}, reject: function (params) {}}]
  // 实现核心啊部分
  // 定义resolve函数作为实参
  let resolveFn = () => {
    // 遍历所有resolve然后依次调用
    for (let value of this.Callbacks) {
      value.resolve()
    }
  }
  let rejectFn = () => {
    // 遍历所有reject然后依次调用
    for (let value of this.Callbacks) {
      value.reject()
    }
  }
  fn(resolveFn, rejectFn) // 函数调用过程传递的是实参
}
// 接下来实现then部分
MyPromise.prototype.then = function (resolve, reject) {
  // console.log(resolve, reject, 12345679) // () => console.log('成功了') () => console.log(`失败了`) 12345679
  this.Callbacks.push({resolve, reject}) // [{resolve: resolve, reject: reject}]
  console.log(this.Callbacks)
}
var p = new MyPromise(function (resolve, reject) {
  // resolve reject是形参 
  // 这里使用定时器模拟一些异步操作比如ajax请求等...
  console.log(6666) // 会立即执行
  setTimeout(() => {
    reject()
  }, 0)
})
p
.then(() => console.log('成功了'), () => console.log(`失败了`))
// then()是不是函数调用 函数调用过程中传递测参数是实参
